package main.com.liuchong.datadeal;

import java.io.*;
import java.util.*;

/**
 * @author: liuchong
 * @create: 2023/3/8 16:11
 * @description:
 */
public class BasicFileDeal implements DataDeal {

    public Map<String,String>[] batchDeduplicate (String fileDirectory){
        // 存放去重数据
        Map<String, String> noDuplicateMap = new LinkedHashMap<>();
        // 存放重复数据
        Map<String, String> duplicateMap = new LinkedHashMap<>();
        return readFile(fileDirectory, noDuplicateMap, duplicateMap);
    }

    public void outPut(String fileDirectory, Map<String,String>[] arrMap) throws IOException {
        for (String value: arrMap[0].values()) {
            System.out.println(value);
        }
        List<Map.Entry<String, String>> noDuplicateList = new ArrayList<>(arrMap[0].entrySet());
        List<Map.Entry<String, String>> duplicateList = new ArrayList<>(arrMap[1].entrySet());
        writeFile(fileDirectory + "Non-duplicate.txt", noDuplicateList);
        writeFile(fileDirectory + "duplicate.txt", duplicateList);
    }

    public Map<String,String>[] singleDeduplicate (String filePath) {
        Map<String, String> noDuplicateMap = new LinkedHashMap<>();
        Map<String, String> duplicateMap = new LinkedHashMap<>();
        return readFile(filePath, noDuplicateMap, duplicateMap);
    }

    /**
     *
     * @param filePath  文件路径或目录
     * @param noDuplicateMap  去重后数据
     * @param duplicateMap  重复数据
     * @return 处理后的数据，长度为2的数组
     */
    private Map<String,String>[] readFile (
            String filePath, Map<String, String> noDuplicateMap, Map<String, String> duplicateMap) {
        File file = new File(filePath);
        if (!file.isDirectory()) {
            try {
                //读取文件
                FileInputStream fis = new FileInputStream(filePath);
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                String temp;
                while ((temp = br.readLine()) != null) {
                    if (temp.length() != 0) {
                        String[] arr = temp.split("\\s+");
                        if (!noDuplicateMap.containsKey(arr[0])) {
                            noDuplicateMap.put(arr[0], temp);
                        } else if (!duplicateMap.containsKey(arr[0])){
                            duplicateMap.put(arr[0], temp);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (file.isDirectory()) {
            String[] fileList = file.list();
            for (int i = 0; i < Objects.requireNonNull(fileList).length; i++) {
                try {
                    //读取文件
                    FileInputStream fis = new FileInputStream(filePath + "/" + fileList[i]);
                    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                    String temp;
                    while ((temp = br.readLine()) != null) {
                        if (temp.length() != 0) {
                            String[] arr = temp.split("\\s+");
                            if (!noDuplicateMap.containsKey(arr[0])) {
                                noDuplicateMap.put(arr[0], temp);
                            } else if (!duplicateMap.containsKey(arr[0])){
                                duplicateMap.put(arr[0], temp);
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return new Map[]{noDuplicateMap, duplicateMap};
    }

    public void writeFile (String filePath, List<Map.Entry<String, String>> list) throws IOException {
        File file = new File(filePath);
        BufferedWriter bf = new BufferedWriter(new FileWriter(file));
        try {
            for (Map.Entry<String, String> entry : list) {
                bf.write(entry.getValue());
                bf.newLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                bf.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
